<%@ page import="schoolsource.sql.SchoolDBSQL" %>
<%@ page import="schoolsource.model.*" %>
<%@ page import="schoolsource.security.SecurityToken" %>
<%@ page import="schoolsource.gui.FormFields" %>
<% SecurityToken st = (SecurityToken) request.getAttribute(FormFields.SESSION);
   String token = (String) st.getValue(FormFields.TOKEN);
//   CommunicationContainer commC[] = (CommunicationContainer[]) st.getValue("communicationContainer");
   ContactContainer commC[] = (ContactContainer[]) st.getValue("communicationContainer");
   UserContainer studentContainer = (UserContainer) st.getValue("studentContainer");
//   StudentContainer child = (StudentContainer) st.getValue("childContainer");
   UserContainer child = (UserContainer) st.getValue("childContainer");
   UserContainer studentClassTeacher[] = (UserContainer[]) st.getValue("studentClassTeacher");
//   UserContainer studentCommTeacher[] = (UserContainer[]) st.getValue("studentCommTeacher");
   UserContainer studentCommTeacher[] = (UserContainer[]) st.getValue("teacherWhoSentMail");
   int classnum = (new Integer((String) st.getValue(FormFields.CLASSNUM))).intValue();
   String loginType = (String) request.getAttribute(FormFields.USRTYPE); 
   String schoolName = (String) request.getAttribute(FormFields.SCHOOLNAME);
   ClassContainer cc = null;
   if(classnum != -2){
    cc = ((ClassContainer[]) st.getValue("classContainer"))[classnum];
   }
   
   boolean parent = (studentContainer.getType().toString().equals(SchoolDBSQL.PARENT));
   int mailnum = (new Integer((String) st.getValue(FormFields.MAILNUM))).intValue(); 
   boolean reply = ("Reply".equals(request.getParameter(FormFields.BUTTONCLICKED)));
      if (!reply) {
        reply = ("Reply".equals(request.getParameter(FormFields.LINKCLICKED)));
      }
%>


<HTML>
  <HEAD>
    <SCRIPT>
    
// the following function, setCaretLocation, was borrowed from
// http://groups.yahoo.com/group/wdf-dom/message/3759 -- Alex Russell
    function setCaretLocation(location, textareaNode){
        // the index of the position you'd like the cursor at
        var loc = parseInt(location);
        // reference a shorter name
        var twe = textareaNode;
        if (twe.setSelectionRange) {
            twe.focus();
            twe.setSelectionRange(loc, loc);
        }
        else if (twe.createTextRange) {
            var range = twe.createTextRange();
            range.collapse(true);
            range.moveEnd('character', loc);
            range.moveStart('character', loc);
            range.select();
        } 
        else { //otherwise try a moz specific hack
            // not sure if these are really necessaray
            twe.value = twe.value;
            twe.blur(); twe.focus();
            // figure out how far "back" to go.
            var dist = parseInt(twe.value.length)-loc;
            // a left arrow char (hopefully?)
            var tchar = String.fromCharCode(37);
            for(var x = 0; x < dist; x++){
                /* see Mozilla LXR for more info about this constructor
                   http://lxr.mozilla.org/seamonkey/source/content/events/src/
                   nsDOMEvent.cpp#1289*/
               var te = document.createEvent("KeyEvents");
               te.initKeyEvent("keypress", true, true,
               null, false, false, false,
               false, tchar.charCodeAt(0),
               tchar.charCodeAt(0));
               twe.dispatchEvent(te);
            }
        }
    }
    
    function initForm() {
        document.forms[0].<%= SchoolDBSQL.MSGBODY %>.focus();
        setCaretLocation(-1, document.forms[0].<%= SchoolDBSQL.MSGBODY %>);
    }
    
    function submitForm(val) {
    	document.forms.mailform.<%= FormFields.LINKCLICKED %>.value = val;
    	document.forms.mailform.<%= FormFields.ASSIGNEDRECIPIENTS %>.value = document.forms.mailform.<%= SchoolDBSQL.CTTOID %>.value;
    	document.forms.mailform.submit();
    }
    
    
    </SCRIPT>
  </HEAD>
  <BODY<%= (reply) ? " onLoad=\"initForm();\"" : "" %>>
    <FORM NAME="mailform" METHOD="POST" onSubmit="return false;">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.ASSIGNEDRECIPIENTS %>" VALUE="">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CURRENT %>" VALUE="<%= FormFields.SUBFRAME %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.TOKEN %>" VALUE="<%= token %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PARAMETER %>" VALUE="">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CLASSNUM %>" VALUE="<%= classnum %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.LINKCLICKED %>" VALUE="Send">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.SCHOOLNAME %>" VALUE="<%= schoolName %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.USRTYPE %>" VALUE="<%= loginType %>">

  <% 
     if (mailnum != -1 && !reply) {
//        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + SchoolDBSQL.CTTOID + "\" VALUE=\"" + studentClassTeacher[classnum].getAssignedID().toString() + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + SchoolDBSQL.CTTOID + "\" VALUE=\"" + commC[mailnum].getToID() + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + SchoolDBSQL.CTFROMID + "\" VALUE=\"" + commC[mailnum].getFromID() + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"FORMFROMPARENT\" VALUE=\"" + (parent ? "TRUE" : "FALSE") + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"FORMFROMSTUDENT\" VALUE=\"" + (parent ? "FALSE" : "TRUE") + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"MAILNUM\" VALUE=\"" + mailnum + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"CLASSID\" VALUE=\"" + commC[mailnum].getClassAssignedID().toString() + "\">");
        out.println("<FONT STYLE=\"font-size:12;font-family:sans-serif;\">");
        out.println("<B>From:&nbsp;&nbsp;</B>" + studentCommTeacher[mailnum].getLastName().toString() + "<br>");
        out.println("<B>Subject:&nbsp;&nbsp;</B>" + commC[mailnum].getMessage().getSubject().toString() + "<br>");
        out.println("<hr>");
//        out.println(commC[mailnum].getMessageBody().toString());
        StringBuffer body = new StringBuffer(commC[mailnum].getMessage().getMessageBody().toString());
        int linebreakIndex = 0;
        for (int i = 0; i < body.length(); i++) {
            if ((int) body.charAt(i) == 13) {
                body.replace(i, i + 1, "<BR>");
            }
        }
        out.println(body.toString());
        out.println("</FONT>");
//        out.println("<CENTER><INPUT TYPE=\"SUBMIT\" NAME=\"BUTTONCLICKED\" VALUE=\"Reply\"></CENTER>");
     }
     else if(reply){
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + SchoolDBSQL.CTTOID + "\" VALUE=\"" + commC[mailnum].getFromID() + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + SchoolDBSQL.CTFROMID + "\" VALUE=\"" + commC[mailnum].getToID() + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + FormFields.FORMFROMPARENT + "\" VALUE=\"" + (parent ? "TRUE" : "FALSE") + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + FormFields.FORMFROMSTUDENT + "\" VALUE=\"" + (parent ? "FALSE" : "TRUE") + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + SchoolDBSQL.CLASSASSIGNEDID + "\" VALUE=\"" + commC[mailnum].getClassAssignedID().toString() + "\">");
        out.println("<TABLE STYLE=\"font-size:11;font-family:sans-serif;\">");
        out.println("<TR>");
        out.println("<TD>");
        out.println("&nbsp;<B>To:&nbsp;&nbsp;</B></TD>");
        out.println("<TD>");
    UserContainer classTeacher = UserContainer.getUserContainer(commC[mailnum].getFromID().toString(), schoolName);
        out.println(classTeacher.getFirstName() + " " + classTeacher.getLastName());
//        out.println(studentClassTeacher[classnum].getFirstName() + " " + studentClassTeacher[classnum].getLastName());
        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD>");
        out.println("&nbsp;<B>From:&nbsp;&nbsp;</B></TD>");
        out.println("<TD>");
        out.println(((parent) ? studentContainer.getFirstName().toString() + " " 
                                + studentContainer.getLastName().toString() + ", Parent of " 
                                + child.getFirstName() + " " + child.getLastName() : 
                                studentContainer.getFirstName().toString() + " " 
                                + studentContainer.getLastName().toString()));
        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD>");
        out.println("&nbsp;<B>Subject:&nbsp;&nbsp;</B></TD>");
        out.println("<TD>");
        String subject;
        if (commC[mailnum].getMessage().getSubject().toString().length() > 2 &&
            commC[mailnum].getMessage().getSubject().toString().substring(0, 3).equals("RE:")) {
            subject = commC[mailnum].getMessage().getSubject().toString();
        }
        else {
        subject = "RE: " + commC[mailnum].getMessage().getSubject().toString();
        }
        out.println("<INPUT TYPE=\"TEXT\" STYLE=\"font-size:11;font-family:sans-serif;\" NAME=\"" + SchoolDBSQL.MSGSUBJECT + "\" VALUE=\"" + subject + "\" SIZE=\"82\"></TD>");
//        out.println("<INPUT TYPE=\"TEXT\" NAME=\"FORMSUBJECT\" VALUE=\"" + subject + "\"></TD>");
        out.println("</TR>");
        out.println("</TABLE>");
        out.println("<hr>");
        out.println("<TEXTAREA STYLE=\"width:100%;font-size:12;font-family:sans-serif;\" NAME=\"" + SchoolDBSQL.MSGBODY + "\" COLS=\"80\" ROWS=\"11\" VALUE=\"\" SCROLLING='NO'>");
        out.println("\n\n<---- original message ---->");
        out.println(commC[mailnum].getMessage().getMessageBody().toString());
        out.println("</TEXTAREA><BR>");
        out.println("<CENTER><INPUT TYPE=\"BUTTON\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Send\" onClick=\"submitForm('Send');\">  ");
        out.println("<INPUT TYPE=\"BUTTON\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Cancel\" onClick=\"submitForm('Cancel');\"></CENTER>");
     }
     else {
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + SchoolDBSQL.CTTOID + "\" VALUE=\"" + studentClassTeacher[classnum].getAssignedID().toString() + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + SchoolDBSQL.CTFROMID + "\" VALUE=\"" + studentContainer.getAssignedID().toString() + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + FormFields.FORMFROMPARENT + "\" VALUE=\"" + (parent ? "TRUE" : "FALSE") + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + FormFields.FORMFROMSTUDENT + "\" VALUE=\"" + (parent ? "FALSE" : "TRUE") + "\">");
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + SchoolDBSQL.CLASSASSIGNEDID + "\" VALUE=\"" + cc.getClassAssignedID().toString() + "\">");
        out.println("<TABLE STYLE=\"font-size:11;font-family:sans-serif;\">");
        out.println("<TR>");
        out.println("<TD>");
        out.println("&nbsp;<B>To:&nbsp;&nbsp;</B></TD>");
        out.println("<TD>");
        out.println(studentClassTeacher[classnum].getFirstName() + " " + studentClassTeacher[classnum].getLastName());
        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD>");
        out.println("&nbsp;<B>From:&nbsp;&nbsp;</B></TD>");
        out.println("<TD>");
        out.println(((parent) ? studentContainer.getFirstName().toString() + " " 
                                + studentContainer.getLastName().toString() + ", Parent of " 
                                + child.getFirstName() + " " + child.getLastName() : 
                                studentContainer.getFirstName().toString() + " " 
                                + studentContainer.getLastName().toString()));
        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD>");
        out.println("&nbsp;<B>Subject:&nbsp;&nbsp;</B></TD>");
        out.println("<TD>");
        String subject;
        subject = "";
        out.println("<INPUT TYPE=\"TEXT\" STYLE=\"font-size:11;font-family:sans-serif;\" NAME=\"" + SchoolDBSQL.MSGSUBJECT + "\" VALUE=\"" + subject + "\" SIZE=\"82\"></TD>");
//        out.println("<INPUT TYPE=\"TEXT\" NAME=\"FORMSUBJECT\" VALUE=\"" + subject + "\"></TD>");
        out.println("</TR>");
        out.println("</TABLE>");
        out.println("<hr>");
        out.println("<TEXTAREA STYLE=\"width:100%;font-size:11;font-family:sans-serif;\" NAME=\"" + SchoolDBSQL.MSGBODY + "\" COLS=\"80\" ROWS=\"11\" VALUE=\"\" SCROLLING='NO'>");
        out.println("</TEXTAREA><BR>");
        out.println("<CENTER><INPUT TYPE=\"BUTTON\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Send\" onClick=\"submitForm('Send');\">  ");
        out.println("<INPUT TYPE=\"BUTTON\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Cancel\" onClick=\"submitForm('Cancel')\"></CENTER>");
     }
     %>
    </FORM>
  </BODY>
</HTML>